/*
 * N900Server.cpp
 *
 *  Created on: Aug 12, 2016
 *      Author: ubuntu
 */


#include "N900Server.h"
#include "N900ClientConnect.h"
#include "../common.h"
#include "../Conf.h"

N900Server::N900Server(int port, int type) : TcpServer(port,  type)
{
}

N900Server::~N900Server()
{
	list<N900DevStatus *>::iterator it = m_lstDevstatus.begin();
	for (; it != m_lstDevstatus.end(); it++) {
		N900DevStatus *pDevstatus = *it;
		delete pDevstatus;
	}
}

ClientConnection *N900Server::on_connect(struct connectop *op, struct sockaddr *addr)
{
	N900ClientConnection *pConnect = new N900ClientConnection(this, op, addr);
	return pConnect;
}

void N900Server::addStatus(N900DevStatus *pStatus)
{
	m_lstDevstatus.push_back(pStatus);
}

void N900Server::delStatus(N900DevStatus *pStatus)
{
	m_lstDevstatus.remove(pStatus);
	delete pStatus;
}

void N900Server::add(void *arg)
{
	N900DevStatus *pStatus = (N900DevStatus *)arg;
	addStatus(pStatus);
}

void N900Server::del(void *arg)
{
	N900DevStatus *pStatus = (N900DevStatus *)arg;
	delStatus(pStatus);
}

void *N900Server::find(void *arg)
{
	if (arg == NULL) {
		return NULL;
	}

	N900DevStatus *pStatus = NULL;
	N900DevStatus *tmp = NULL;

	unsigned int obdid = *((unsigned int *)arg);
	list<N900DevStatus *>::iterator it = m_lstDevstatus.begin();
	for (; it != m_lstDevstatus.end(); it++) {
		tmp = *it;
		if (tmp->getObdid() == obdid) {
			pStatus = tmp;
			break;
		}
	}

	return (void *)pStatus;
}

